#include<iostream>

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */

int findmax(int** grid, int i, int j)
{
    int a = (grid[i - 1][j - 1] > grid[i - 1][j] ? grid[i - 1][j - 1] : grid[i - 1][j]);
    int b = (grid[i][j - 1] > grid[i][j] ? grid[i][j - 1] : grid[i][j]);
    int c = (grid[i + 1][j - 1] > grid[i + 1][j] ? grid[i + 1][j - 1] : grid[i + 1][j]);
    a = (a > b ? a : b);
    a = (a > c ? a : c);
    b = (grid[i - 1][j + 1] > grid[i][j + 1] ? grid[i - 1][j + 1] : grid[i][j + 1]);
    b = b > grid[i + 1][j + 1] ? b : grid[i + 1][j + 1];
    return a > b ? a : b;
}
int** largestLocal(int** grid, int gridSize, int* gridColSize, int* returnSize, int** returnColumnSizes) {
    *returnSize = gridSize - 2;
    *returnColumnSizes = (int*)calloc(gridSize - 2, sizeof(int));
    int** res = (int**)malloc(sizeof(int*) * (gridSize - 2));
    for (int i = 1; i < gridSize - 1; i++)
    {
        res[i - 1] = (int*)malloc(sizeof(int) * (gridSize - 2));
        (*returnColumnSizes)[i - 1] = gridSize - 2;
        for (int j = 1; j < gridSize - 1; j++)
        {
            res[i - 1][j - 1] = findmax(grid, i, j);
        }
    }
    return res;
}